package com.dubbo.oneclass;

import org.apache.dubbo.config.*;
import org.apache.zookeeper.server.ServerConfig;
import org.junit.Test;

import java.io.IOException;

public class DubboSingletonRemote {

    /**
     * 客户端
     */
    @Test
    public void client() {

        ReferenceConfig<UserService> referenceConfig = new ReferenceConfig<>();
        ApplicationConfig applicationConfig = new ApplicationConfig("my-client");
        referenceConfig.setApplication(applicationConfig);
        // dubbo地址后面是实现类报名，在服务端启动日志可以看到
        referenceConfig.setUrl("dubbo://127.0.0.1:8080/com.dubbo.oneclass.UserService");
        referenceConfig.setInterface(UserService.class);
        UserService userService = referenceConfig.get();
        System.out.println(userService.getUser(111));
    }


    /**
     * 服务端
     */
    @Test
    public void server() throws IOException {

        ServiceConfig serviceConfig = new ServiceConfig();

        ApplicationConfig applicationConfig = new ApplicationConfig("my-server");
        serviceConfig.setApplication(applicationConfig);
        // 创建dubbo协议
        ProtocolConfig protoConfig = new ProtocolConfig("dubbo");
        protoConfig.setPort(8080);
        serviceConfig.setProtocol(protoConfig);
        // 注册集群，单机版用不到
        RegistryConfig registryConfig = new RegistryConfig(RegistryConfig.NO_AVAILABLE);
        serviceConfig.setRegistry(registryConfig);
        // 设置接口和实现类
        serviceConfig.setInterface(UserService.class);
        serviceConfig.setRef(new UserServiceImpl(8080));

        // 开启服务
        serviceConfig.export();

        System.in.read();


    }

}
